package com.jeecg.sms.job;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jeecgframework.core.util.DynamicDBUtil;
import org.jeecgframework.core.util.HttpRequest;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.web.system.pojo.base.TSType;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jeecg.sms.service.RptSmsSendServiceI;

@Service("zahmmessageTask")
public class ZAHMMessageJob {

	private final static Logger  logger = Logger.getLogger(MessageJob.class);
	@Autowired
	private RptSmsSendServiceI rptSmsSendService;
	@Autowired
	private SystemService systemService;
	/**
	 * 生成五大航空完成计划短信
	 */
	public void run() {
		logger.info("===================五大航空完成计划短信生成定时任务开始===================");
		String sql="select to_char(t.rq,'yyyy\"年\"mm\"月\"dd\"日\"') as d,"+
				"(select count(1) from AS_FLIGHTINFO_SCHE_CAN where  to_char(scheexecdate,'yyyymmdd')=to_char(t.rq,'yyyymmdd')) as ap,"+
				"(select count(hbh) FROM flt_cd_i_aomip a WHERE a.state =0 and a.sjsj LIKE to_char(t.rq,'yyyy-mm-dd')||'%') as ad,"+
				"(select round(b.c*100/a.c,2) from (select count(1) c from AS_FLIGHTINFO_SCHE_CAN where  to_char(scheexecdate,'yyyymmdd')=to_char(t.rq,'yyyymmdd')) a,(select count(hbh) c FROM flt_cd_i_aomip a WHERE a.state =0 and a.sjsj LIKE to_char(t.rq,'yyyy-mm-dd')||'%') b) ar,"+
				"(select nvl(sum(a.cont),0) from flt_all_data a where a.qjrq = to_char(t.rq,'yyyymmdd') and a.state =1) as ac,"+
				"(select nvl(sum(a.cont),0) from flt_all_data a where a.qjrq = to_char(t.rq,'yyyymmdd') and a.state =1 and a.abrs = '天气原因') as acw,"+
				"(select count(1) from AS_FLIGHTINFO_SCHE_CAN where cyr='CZ' and to_char(scheexecdate,'yyyymmdd')=to_char(t.rq,'yyyymmdd')) as czp,"+
				"(select count(hbh) FROM flt_cd_i_aomip a WHERE a.state =0 and awcd='CZ' and a.sjsj LIKE to_char(t.rq,'yyyy-mm-dd')||'%') as czd,"+
				"(select round(b.c*100/a.c,2) from (select count(1) c from AS_FLIGHTINFO_SCHE_CAN where cyr='CZ' and to_char(scheexecdate,'yyyymmdd')=to_char(t.rq,'yyyymmdd')) a,(select count(hbh) c FROM flt_cd_i_aomip a WHERE a.state =0 and awcd='CZ' and a.sjsj LIKE to_char(t.rq,'yyyy-mm-dd')||'%') b) czr,"+
				"(select nvl(sum(a.cont),0) from flt_all_data a where a.awcd='CZ' and a.qjrq = to_char(t.rq,'yyyymmdd') and a.state =1) as czc,"+
				"(select nvl(sum(a.cont),0) from flt_all_data a where a.awcd='CZ' and a.qjrq = to_char(t.rq,'yyyymmdd') and a.state =1 and a.abrs = '天气原因') as czcw,"+
				"(select count(1) from AS_FLIGHTINFO_SCHE_CAN where cyr='CA' and to_char(scheexecdate,'yyyymmdd')=to_char(t.rq,'yyyymmdd')) as cap,"+
				"(select count(hbh) FROM flt_cd_i_aomip a WHERE a.state =0 and awcd='CA' and a.sjsj LIKE to_char(t.rq,'yyyy-mm-dd')||'%') as cad,"+
				"(select round(b.c*100/a.c,2) from (select count(1) c from AS_FLIGHTINFO_SCHE_CAN where cyr='CA' and to_char(scheexecdate,'yyyymmdd')=to_char(t.rq,'yyyymmdd')) a,(select count(hbh) c FROM flt_cd_i_aomip a WHERE a.state =0 and awcd='CA' and a.sjsj LIKE to_char(t.rq,'yyyy-mm-dd')||'%') b) car,"+
				"(select nvl(sum(a.cont),0) from flt_all_data a where a.awcd='CA' and a.qjrq = to_char(t.rq,'yyyymmdd') and a.state =1) as cac,"+
				"(select nvl(sum(a.cont),0) from flt_all_data a where a.awcd='CA' and a.qjrq = to_char(t.rq,'yyyymmdd') and a.state =1 and a.abrs = '天气原因') as cacw,"+
				"(select count(1) from AS_FLIGHTINFO_SCHE_CAN where cyr='MU' and to_char(scheexecdate,'yyyymmdd')=to_char(t.rq,'yyyymmdd')) as mup,"+
				"(select count(hbh) FROM flt_cd_i_aomip a WHERE a.state =0 and awcd='MU' and a.sjsj LIKE to_char(t.rq,'yyyy-mm-dd')||'%') as mud,"+
				"(select round(b.c*100/a.c,2) from (select count(1) c from AS_FLIGHTINFO_SCHE_CAN where cyr='MU' and to_char(scheexecdate,'yyyymmdd')=to_char(t.rq,'yyyymmdd')) a,(select count(hbh) c FROM flt_cd_i_aomip a WHERE a.state =0 and awcd='MU' and a.sjsj LIKE to_char(t.rq,'yyyy-mm-dd')||'%') b) mur,"+
				"(select nvl(sum(a.cont),0) from flt_all_data a where a.awcd='MU' and a.qjrq = to_char(t.rq,'yyyymmdd') and a.state =1) as muc,"+
				"(select nvl(sum(a.cont),0) from flt_all_data a where a.awcd='MU' and a.qjrq = to_char(t.rq,'yyyymmdd') and a.state =1 and a.abrs = '天气原因') as mucw,"+
				"(select count(1) from AS_FLIGHTINFO_SCHE_CAN where cyr='HU' and to_char(scheexecdate,'yyyymmdd')=to_char(t.rq,'yyyymmdd')) as hup,"+
				"(select count(hbh) FROM flt_cd_i_aomip a WHERE a.state =0 and awcd='HU' and a.sjsj LIKE to_char(t.rq,'yyyy-mm-dd')||'%') as hud,"+
				"(select round(b.c*100/a.c,2) from (select count(1) c from AS_FLIGHTINFO_SCHE_CAN where cyr='HU' and to_char(scheexecdate,'yyyymmdd')=to_char(t.rq,'yyyymmdd')) a,(select count(hbh) c FROM flt_cd_i_aomip a WHERE a.state =0 and awcd='HU' and a.sjsj LIKE to_char(t.rq,'yyyy-mm-dd')||'%') b) hur,"+
				"(select nvl(sum(a.cont),0) from flt_all_data a where a.awcd='HU' and a.qjrq = to_char(t.rq,'yyyymmdd') and a.state =1) as huc,"+
				"(select nvl(sum(a.cont),0) from flt_all_data a where a.awcd='HU' and a.qjrq = to_char(t.rq,'yyyymmdd') and a.state =1 and a.abrs = '天气原因') as hucw,"+
				"(select count(1) from AS_FLIGHTINFO_SCHE_CAN where cyr='ZH' and to_char(scheexecdate,'yyyymmdd')=to_char(t.rq,'yyyymmdd')) as zhp,"+
				"(select count(hbh) FROM flt_cd_i_aomip a WHERE a.state =0 and awcd='ZH' and a.sjsj LIKE to_char(t.rq,'yyyy-mm-dd')||'%') as zhd,"+
				"(select round(b.c*100/a.c,2) from (select count(1) c from AS_FLIGHTINFO_SCHE_CAN where cyr='ZH' and to_char(scheexecdate,'yyyymmdd')=to_char(t.rq,'yyyymmdd')) a,(select count(hbh) c FROM flt_cd_i_aomip a WHERE a.state =0 and awcd='ZH' and a.sjsj LIKE to_char(t.rq,'yyyy-mm-dd')||'%') b) zhr,"+
				"(select nvl(sum(a.cont),0) from flt_all_data a where a.awcd='ZH' and a.qjrq = to_char(t.rq,'yyyymmdd') and a.state =1) as zhc,"+
				"(select nvl(sum(a.cont),0) from flt_all_data a where a.awcd='ZH' and a.qjrq = to_char(t.rq,'yyyymmdd') and a.state =1 and a.abrs = '天气原因') as zhcw"+
				" from (select (sysdate-1) as rq from dual) t";
		List<Map<String,Object>> dataList =  DynamicDBUtil.findList("production_db", sql);
		if(dataList.size()>0)
		{
			String content= dataList.get(0).get("D")+"民航局批复计划一共"+dataList.get(0).get("AP")+"班，实际执行一共"+dataList.get(0).get("AD")+"班，总执行率为"+dataList.get(0).get("AR")+"%。"+
					"与日计划比较取消航班总量"+dataList.get(0).get("AC")+"班，其中：因天气原因取消"+dataList.get(0).get("ACW")+"班。\r\n"+
					"南航：批复"+dataList.get(0).get("CZP")+"班，实际执行"+dataList.get(0).get("CZD")+"班，执行率"+dataList.get(0).get("CZR")+"%。与日计划比较取消航班总量"+dataList.get(0).get("CZC")+"班，其中：因天气原因取消"+dataList.get(0).get("CZCW")+"班；\r\n"+
					"深航：批复"+dataList.get(0).get("ZHP")+"班，实际执行"+dataList.get(0).get("ZHD")+"班，执行率"+dataList.get(0).get("ZHR")+"%。与日计划比较取消航班总量"+dataList.get(0).get("ZHC")+"班，其中：因天气原因取消"+dataList.get(0).get("ZHCW")+"班；\r\n"+
					"东航：批复"+dataList.get(0).get("MUP")+"班，实际执行"+dataList.get(0).get("MUD")+"班，执行率"+dataList.get(0).get("MUR")+"%。与日计划比较取消航班总量"+dataList.get(0).get("MUC")+"班，其中：因天气原因取消"+dataList.get(0).get("MUCW")+"班；\r\n"+
					"国航：批复"+dataList.get(0).get("CAP")+"班，实际执行"+dataList.get(0).get("CAD")+"班，执行率"+dataList.get(0).get("CAR")+"%。与日计划比较取消航班总量"+dataList.get(0).get("CAC")+"班，其中：因天气原因取消"+dataList.get(0).get("CACW")+"班；\r\n"+
					"海航：批复"+dataList.get(0).get("HUP")+"班，实际执行"+dataList.get(0).get("HUD")+"班，执行率"+dataList.get(0).get("HUR")+"%。与日计划比较取消航班总量"+dataList.get(0).get("HUC")+"班，其中：因天气原因取消"+dataList.get(0).get("HUCW")+"班。\r\n"+
					"（白云机场运行控制中心）";
			//发送信息到接口
			
			//五大航空计划完成短信接收人
			List<TSType> typeList = ResourceUtil.allTypes.get("0002");
			String phones="";
			int index = 0;
			if (typeList != null && !typeList.isEmpty()) {
				for (TSType type : typeList) {
					if (index>0) phones +=",";
					phones +=  type.getTypecode();
					index++;
				}
			}
			//短信接收地址URL和密码
			List<TSType> typeList2 = ResourceUtil.allTypes.get("0001");
			String url="";
			String password="";
			if (typeList != null && !typeList.isEmpty()) {
				for (TSType type : typeList2) {
					if (type.getTypename().equals("url")) 
						url = type.getTypecode();
					if (type.getTypename().equals("password")) 
						password=type.getTypecode();
				}
			}
			try {
				content =java.net.URLEncoder.encode(content,"utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			String param ="password="+password+"&text="+content+"&recipient="+phones+"&encoding=U";

			String result =HttpRequest.sendGet(url, param);
			logger.info(result);
		}
		else
		{
			logger.info("没有查询到五大航空完成计划数据===================");
		}
		logger.info("===================五大航空完成计划短信生成定时任务结束===================");
	}
}
